﻿
using RayDen.RayEngine.Core;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Engines.MCMC
{
    public class PssMltPathBuffer : PathBuffer<PrimarySampleSpacePathSampler>
    {
        internal PssMltEngineController controller;
        public PssMltPathBuffer() { }
        

        public PssMltPathBuffer(int maxPaths, RayEngineScene scene, ImageFilmBase agg, SamplingContext sc)
            : base(maxPaths, scene, agg, sc)
        {
            controller = new PssMltEngineController();
            controller.Init(agg.Width, agg.Height);
        }

        protected override PathSamplerBase CreatePathSampler()
        {
            return new PrimarySampleSpacePathSampler()
            {
                SurfaceSampler = this.surfaceSampler, 
                pathIntegrator = this, 
                LightSampler = this.lightSampler, 
                contributionMap = this.controller.imagePlane
            };
        }

        protected static void Print(string msg)
        {
#if VERBOSE
            RayDen.Library.Components.SystemComponents.Tracer.TraceLine(msg);
#endif

        }

        public override void AdvancePaths(RayBuffer rayBuffer)
        {
            base.AdvancePaths(rayBuffer);
#if TRACE_PATHS
            Print("--------------");
            Print(string.Format("{0} samplers in LargeStep",
                this.paths.Cast<PathSampler>().Count(item => item.SamplerState == MCMCSamplerState.LargeStep)));
            Print(string.Format("{0} samplers in SmallStep",
                this.paths.Cast<PathSampler>().Count(item => item.SamplerState == MCMCSamplerState.SmallStep)));
            Print(string.Format("{0} average rejection",
                this.paths.Cast<PathSampler>().Average(item => item.rejectCount)));
            Print(string.Format("{0} Total Samples",this.paths.Count));
            Print("Importance ready : " + (Sampler as MCMCSampler).ImportanceReady);
            Print("--------------");
            
#endif
        }
    }
}